<?php
class PostsController extends AppController {
	
	public $helpers = array('Html', 'Form', 'Session', 'Time');

	public $components = array('Session');
	
	public function index() {
// 		$this->set("title_for_layout", "All Posts");
		$this->loadModel('Photo');
		$posts = $this->paginate();
// 		$this->set('posts', $this->paginate());
// 		$this->log($this->paginate(), 'debug');
		foreach ($posts as &$post) {
			$photos = $this->Photo->findAllByPostId($post['Post']['_id']);
			foreach ($photos as $photo) {
				$post['Post']['photos'][] = $photo;
			}
			unset($photo);
		}
// 		$this->log($posts, 'debug');
		unset($post);
		$this->set('posts', $posts);
	}
	
	public function view($id = null) {
		if (!$id) throw new NotFoundException(__('Invalid Post'));
		$post = $this->Post->findById($id);
		if (!$post) throw new NotFoundException(__('Invalid Post'));
		$this->set('post', $post);
	}
	
	public function add() {
		$this->loadModel('Photo');
		
		if ($this->request->is('post')) {
			$this->Post->create();
			$this->Photo->create();
			
			$post_data['Post'] = $this->request->data['Post'];
			$post_data['Post']['likes'] = 0;
			$this->log($post_data, 'debug');
			
			if ($this->Post->save($post_data)) {
				$post_id = $this->Post->getLastInsertID();
				$i = 0;
// 				$photo_data['Photo'] = $this->request->data['Post']['photos'];
				foreach ($this->request->data['Post']['photos'] as $photo) {
					$photo_data['Photo'][$i]['path'] = $photo;
					$photo_data['Photo'][$i++]['post_id'] = $post_id;
				}
				unset($photo);
				unset($i);
				$this->log($photo_data, 'debug');
				
				if ($this->Photo->saveMany($photo_data['Photo'])) {
					$this->Session->setFlash(__('Saved'));
					return $this->redirect(array('action' => 'index'));
				} else {
					$this->Post->delete($post_id);
				}
				
			}
			$this->Session->setFlash(__('Failed'));
		}
	}
	
	public function edit() {
            $data = $this->request->input('json_decode');
            
            $id=$data->id;
            $this->log($data,'debug');
                    if (!$id) {
                        throw new NotFoundException(__('Invalid post'));
                    }

                    $post = $this->Post->findById($id);
                    if (!$post) {
                        throw new NotFoundException(__('Invalid post'));
                    }
                    $post['Post']['likes'] = $data->time;
                    
                        if ($this->Post->save($post)) {
                            $this->Session->setFlash(__('Your post has been updated.'));
                            return $this->redirect(array('action' => 'index'));
                        }
                        $this->Session->setFlash(__('Unable to update your post.'));
                    

                    
                    }
                    public function like() {
            $data = $this->request->input('json_decode');
            
            $id=$data->id;
            $this->log($data,'debug');
                    if (!$id) {
                        throw new NotFoundException(__('Invalid post'));
                    }

                    $post = $this->Post->findById($id);
                    if (!$post) {
                        throw new NotFoundException(__('Invalid post'));
                    }
                    $post['Post']['likes'] = $post['Post']['likes']+1;
                    
                        if ($this->Post->save($post)) {
                           // $this->Session->setFlash(__('Your post has been updated.'));
                         //  return $this->redirect(array('action' => 'index'));
                        }
                        //$this->Session->setFlash(__('Unable to update your post.'));
                    $this->autoRender = false;
                          return $post['Post']['likes'];
                    
                    }


}
